package com.aloda.controller.user;

import cn.hutool.core.bean.BeanUtil;
import com.aloda.constant.ErrorCode;
import com.aloda.dto.userSuggest.UserSuggestAddDTO;
import com.aloda.exception.BaseException;
import com.aloda.result.Result;
import com.aloda.service.UserSuggestService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController("suggest-user")
@RequestMapping("/user/suggest")
public class UserSuggestController {

    @Resource
    private UserSuggestService userSuggestService;

    /**
     * 用户端新增建议
     * @param userSuggestAddDTO
     * @return
     */
    @PostMapping
    public Result addUserSuggest(@Validated @RequestBody UserSuggestAddDTO userSuggestAddDTO, BindingResult bindingResult) {
        log.info("客户端新增建议:{}",userSuggestAddDTO);
        if (BeanUtil.isEmpty(userSuggestAddDTO)) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        if (bindingResult.hasErrors()) {
            StringBuilder errorMessage = new StringBuilder();
            bindingResult.getFieldErrors().forEach(error -> {
                errorMessage.append(error.getDefaultMessage()).append("; ");
            });
           return Result.error(errorMessage.toString());
        }
        userSuggestService.add(userSuggestAddDTO);
        return Result.success();
    }
}
